package com.bsg.upm.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.bsg.upm.dto.DefinitionSubServSoftwareDto;
import com.bsg.upm.entity.DefinitionSubServSoftwareEntity;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;

/**
 * 软件管理业务处理类
 * 
 * @author HCK
 *
 */
@Service
public class SoftwareService extends BaseService {

	/**
	 * 软件查询
	 * 
	 * @return RespJson请求结果对象
	 * @throws APIException
	 *             API异常处理类
	 */
	public RespJson list() throws APIException {
		try {
			// 结果集
			JSONArray results = new JSONArray();

			// 获取指定条件的软件
			List<DefinitionSubServSoftwareEntity> definitionSubServSoftwares = definitionSubServSoftwareDao.list(null);

			for (DefinitionSubServSoftwareEntity software : definitionSubServSoftwares) {
				DefinitionSubServSoftwareDto softwareDto = new DefinitionSubServSoftwareDto();
				softwareDto.setId(software.getId());
				softwareDto.setName(software.getDefinitionSubServName());
				softwareDto.setMajorVersion(String.valueOf(software.getMajorVersion()));
				softwareDto.setMinorVersion(String.valueOf(software.getMinorVersion()));
				results.add(softwareDto);
			}

			return RespJsonFactory.buildOK(results);
		} catch (Exception e) {
			logger.error("软件查询异常", e);
			throw new APIException("软件查询异常:" + e.getMessage());
		}
	}
}
